﻿using CalOut;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_IntoTxt : Form_BaseAlone
    {
        public Form_IntoTxt(string FormText, Form_NewMain form_NewMain)
        {
            InitializeComponent();
            SetComponent(FormText);
            this.form_NewMain = form_NewMain;
        }
        private Form_NewMain form_NewMain;
        private Regex regexZheng = new Regex("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
        private Regex regexFu = new Regex("^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$");
        private DataTable dataTable;
        private string EquipmentId;
        private int ChannelNo;
        private string CalType;
        private string ProjectId;
        private string[] txtData;
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase, class_MySqlDataBaseOld;
        private void SetComponent(string FormText)
        {
            base.SetComponent(FormText, "I1007");
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;
            base.EnableOutButton(false);
            base.SetAdd("选择导入文件", "选择导入文件");
            base.CloseEditButton();
            base.CloseDeleteButton();
            base.EnableViewButton(true, "计算数据并导入");

            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBaseOld = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , "yantu"
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
        }
        private double[,] GetTwoArrayDefaultValue(string paramName, int sensorNo, string mainId)
        {
            int len = 6;
            double[,] myResult = new double[len, len];
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName = '{2}'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                int Row = 0;
                int Col = 0;
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Col = i / len;
                    Row = i % len;
                    myResult[Col, Row] = double.Parse(dataTable.Rows[i][0].ToString());
                }
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }
        private double[] GetOneArrayDefaultValue(string paramName, int sensorNo, string mainId)
        {
            double[] myResult;
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName like '{2}%'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            myResult = new double[dataTable.Rows.Count];
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                    myResult[i] = double.Parse(dataTable.Rows[i][0].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }

        private double GetDefaultValue(string paramName, int sensorNo, string mainId)
        {
            double myResult = 0;
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName = '{2}'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                myResult = double.Parse(dataTable.Rows[0][0].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }

        private double[] GetValue(string paramName, int sensorNo, string mainId)
        {
            double[] vs = new double[2];
            string Sql = string.Format(@"SELECT beginValue,endValue
FROM vou_equipmentparamer
WHERE paramType = '传感器上下限'
AND sensorNo = {0}
AND mainId = {1}
AND paramName = '{2}'
ORDER BY paramName DESC"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                vs[0] = double.Parse(dataTable.Rows[0][0].ToString());
                vs[1] = double.Parse(dataTable.Rows[0][1].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return vs;
        }
        private void ToData()
        {
            if (txtData != null && txtData.Length > 0)
            {
                int rowCount = txtData.Length;
                if (rowCount > 0)
                {
                    form_NewMain.SetProgressMax(txtData.Length);
                    waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

                    #region 创建数据集
                    dataTable = new DataTable();
                    DataColumn dataColumn = new DataColumn("no");
                    dataColumn.Caption = "序号";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);

                    dataColumn = new DataColumn("id");
                    dataColumn.Caption = "批次号";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);

                    dataColumn = new DataColumn("dataTime");
                    dataColumn.Caption = "数据时间";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);

                    dataColumn = new DataColumn("count");
                    dataColumn.Caption = "数量";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);

                    dataColumn = new DataColumn("ProjectId");
                    dataColumn.Caption = "项目编号";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);
                    dataColumn = new DataColumn("EquipmentId");
                    dataColumn.Caption = "设备编号";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);
                    dataColumn = new DataColumn("ChannelNo");
                    dataColumn.Caption = "通道编号";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);
                    dataColumn = new DataColumn("CalType");
                    dataColumn.Caption = "计算类型";
                    dataColumn.DataType = Type.GetType("System.String");
                    dataTable.Columns.Add(dataColumn);

                    for (int i = 0; i < 28; i++)
                    {
                        dataColumn = new DataColumn("data" + (i + 1));
                        dataColumn.Caption = "数据" + (i + 1);
                        dataColumn.DataType = Type.GetType("System.String");
                        dataTable.Columns.Add(dataColumn);
                    }
                    #endregion

                    #region 校验
                    int NowCount = 1;
                    List<int> vs = new List<int>();
                    for (int i = 0; i < rowCount; i++)
                    {
                        string data = txtData[i].Trim();
                        data = data.Replace("    ", " ");
                        data = data.Replace("   ", " ");
                        data = data.Replace("  ", " ");
                        string[] temp = data.Split(' ');
                        if (temp.Length == 17 || temp.Length == 31)
                        {
                            string TimeSign = DateTime.Now.ToString("yyyyMM");
                            DataRow dataRow = dataTable.NewRow();   //插入数据
                            dataRow["no"] = NowCount++;
                            dataRow["id"] = TimeSign + "_" + temp[0].Trim().Replace("-", "") + temp[1].Trim().Replace("-", "")
                                + ProjectId
                                + EquipmentId
                                + ChannelNo.ToString();
                            dataRow["dataTime"] = temp[0].Trim() + " " + temp[1].Trim().Replace("-", ":");
                            dataRow["count"] = temp[2].Trim();
                            dataRow["ProjectId"] = ProjectId;
                            dataRow["EquipmentId"] = EquipmentId;
                            dataRow["ChannelNo"] = ChannelNo;
                            dataRow["CalType"] = CalType;
                            int dataCount = 0;
                            if (temp.Length == 31)
                            {
                                dataCount = 28;
                            }
                            if (temp.Length == 17)
                            {
                                dataCount = 14;
                            }
                            for (int k = 0; k < 28; k++)
                            {
                                if (temp.Length == 31)
                                {
                                    dataRow["data" + (k + 1)] = temp[2 + k + 1].Trim();
                                }
                                if (temp.Length == 17)
                                {
                                    if (k < 14)
                                        dataRow["data" + (k + 1)] = temp[2 + k + 1].Trim();
                                    else
                                        dataRow["data" + (k + 1)] = "0";
                                }
                            }
                            dataTable.Rows.Add(dataRow);
                        }
                        else
                            vs.Add(i + 1);
                        form_NewMain.SetProgressPosition(i + 1);
                    }
                    #endregion

                    waitDialogForm.Close();
                    form_NewMain.SetProgressOK();

                    if (dataTable.Rows.Count < rowCount)
                    {
                        if (rowCount - dataTable.Rows.Count > 0)
                        {
                            if (MessageBox.Show(string.Format("文件中数据量为【{0}】;\r\n合法的数据量为【{1}】;\r\n发现条【{2}】数据不合法！\r\n---您要查看错误日志吗？"
                                , rowCount
                                , dataTable.Rows.Count
                                , rowCount - dataTable.Rows.Count)
                                , "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                            {
                                StringBuilder stringBuilder = new StringBuilder();
                                stringBuilder.AppendFormat("发现如下【{0}】条错误数据：\r\n", rowCount - dataTable.Rows.Count);
                                for (int i = 0; i < vs.Count; i++)
                                {
                                    stringBuilder.AppendFormat("   第【{0}】条，有错误！\r\n", vs[i].ToString());
                                }
                                string fileName = Class_Tool.WriteErrorToTxt(stringBuilder.ToString());
                                if (File.Exists(fileName))
                                    System.Diagnostics.Process.Start(@fileName);

                                stringBuilder.Clear();
                                stringBuilder.Length = 0;
                            }
                        }
                        else
                            MessageBox.Show(string.Format("成功能导入【{0}】条数据！\r\n"
                                , rowCount)
                                , "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    vs.Clear();
                }
            }

        }
        public override int AddData()
        {
            string FileName = null;
            Form_IntoTxtBase form_IntoTxtBase = new Form_IntoTxtBase();
            if (form_IntoTxtBase.ShowDialog() == DialogResult.OK)
            {
                EquipmentId = form_IntoTxtBase.EquipmentId;
                ChannelNo = form_IntoTxtBase.ChannelNo;
                CalType = form_IntoTxtBase.CalType;
                ProjectId = form_IntoTxtBase.ProjectId;
                FileName = form_IntoTxtBase.FileName;
            }
            form_IntoTxtBase.Close();
            form_IntoTxtBase.Dispose();

            if (FileName != null)
            {
                txtData = readTxt(FileName);
            }
            if (txtData != null && txtData.Length > 0)
            {
                ToData();
                Finder(1);
            }
            txtData = null;
            return -1;
        }
        private string[] readTxt(string filePath)
        {
            return File.ReadAllLines(filePath, Encoding.Default);
        }
        public override int EditData(object id)
        {
            int index = -1;
            return index;
        }
        private bool IsNumber(string content)
        {
            bool finder = regexZheng.IsMatch(content);
            if (!finder)
                finder = regexFu.IsMatch(content);
            return finder;
        }
        private StringBuilder stringBuilder = new StringBuilder();
        private bool CheckData()
        {
            //ChannelNo
            double[] waveUp = new double[2];
            double[] powerUp = new double[2];
            double[] waveDown = new double[2];
            double[] powerDown = new double[2];
            for (int i = 0; i < 2; i++)
            {
                waveUp[i] = 0;
                powerUp[i] = 0;
                waveDown[i] = 0;
                powerDown[i] = 0;
            }

            stringBuilder.Clear();
            bool finder = dataTable != null && dataTable.Rows.Count > 0 ? true : false;
            if (finder)
            {
                int ChannelNo = int.Parse(dataTable.Rows[0]["ChannelNo"].ToString());
                string mainId = dataTable.Rows[0]["EquipmentId"].ToString();
                int sensorNo = ChannelNo;
                waveUp = this.GetValue("wave", sensorNo, mainId);
                powerUp = this.GetValue("power", sensorNo, mainId);
                sensorNo += 6;
                waveDown = this.GetValue("wave", sensorNo, mainId);
                powerDown = this.GetValue("power", sensorNo, mainId);

                form_NewMain.SetProgressMax(dataTable.Rows.Count);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    bool ErrorSign = false;
                    List<CheckDataClass> vs = new List<CheckDataClass>();
                    for (int k = 0; k < dataTable.Columns.Count; k++)
                    {
                        if (k > 7)
                        {
                            if (dataTable.Rows[i][k].ToString().Length > 0 && dataTable.Rows[i][k].ToString() != "0")
                            {
                                if (!IsNumber(dataTable.Rows[i][k].ToString()))
                                {
                                    ErrorSign = ErrorSign || true;
                                    finder = finder && false;
                                    vs.Add(new CheckDataClass(k - 6, "不是数字"));
                                }
                                else
                                {
                                    double myDouble = double.Parse(dataTable.Rows[i][k].ToString());
                                    if (myDouble != 0)
                                    {
                                        double beginValue = 0;
                                        double endValue = 0;
                                        if (k > 7 && k < 22)
                                        {
                                            if (k % 2 == 0)
                                            {
                                                beginValue = waveUp[0];
                                                endValue = waveUp[1];
                                            }
                                            else
                                            {
                                                beginValue = powerUp[0];
                                                endValue = powerUp[1];
                                            }
                                        }
                                        else
                                        {
                                            if (k % 2 == 0)
                                            {
                                                beginValue = waveDown[0];
                                                endValue = waveDown[1];
                                            }
                                            else
                                            {
                                                beginValue = powerDown[0];
                                                endValue = powerDown[1];

                                            }
                                        }
                                        if (myDouble < beginValue || myDouble > endValue)
                                        {
                                            ErrorSign = ErrorSign || true;
                                            finder = finder && false;
                                            vs.Add(new CheckDataClass(k - 6
                                                , string.Format("数值超过上下限:{0}---{1}", beginValue, endValue)));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (ErrorSign)
                    {
                        stringBuilder.AppendFormat("【第{0}行】的数据，有错误:"
                            , i + 1);
                        foreach (CheckDataClass item in vs)
                            stringBuilder.AppendFormat(" 数据【{0}】-{1}", item.No, item.ErrorMessage);
                        stringBuilder.Append(";\r\n");
                    }
                    vs.Clear();
                    form_NewMain.SetProgressPosition(i + 1);
                }
                form_NewMain.SetProgressOK();
            }
            if (!finder)
            {
                if (stringBuilder.Length > 0)
                {
                    Class_Tool.WriteCheckToTxt(stringBuilder.ToString());
                }
            }
            return finder;
        }
        public override void ViewData(object id)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                if (CheckData())
                {
                    int InsertCount = 0;
                    int index = 0;
                    string TimeSign = DateTime.Now.ToString("yyyyMM");
                    form_NewMain.SetProgressMax(dataTable.Rows.Count);

                    #region 作准备
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        #region 删除input_data
                        class_MySqlDataBase.ExecuteSql(string.Format(@"DELETE
                            FROM input_data{1}
                            WHERE batchId = '{0}'"
                            , dataRow["id"].ToString()
                            , string.Format("_{0}", TimeSign)));
                        #endregion

                        #region SQL
                        string Sql = string.Format(@"INSERT INTO input_data{0}(
                        id,  calCount,  dataTime,  batchId,  projectId,  equipmentId,  channelNo,
                        calType,  count,  data1,  data2,  data3,  data4,  data5,  data6,  data7,
                        data8,  data9,  data10,  data11,  data12,  data13,  data14,  data15,  data16,
                        data17,  data18,  data19,  data20,  data21,  data22,  data23,
                        data24,  data25,  data26,  data27,  data28)
                        VALUES(  '{1}',  '{2}',  '{3}',  '{4}',  '{5}',  '{6}',  '{7}',
                        '{8}',  {9},  {10},  {11},  {12},  {13},  {14},  {15},  {16},
                        {17},  {18},  {19},  {20},  {21},  {22},  {23},  {24},  {25},
                        {26},  {27},  {28},  {29},  {30},  {31},  {32},
                        {33},  {34},  {35},  {36},  {37})"
                        , string.Format("_{0}", TimeSign)
                        , string.Format("{0}_{1}{2}", TimeSign, Class_Tool.GetKey("IN"), dataRow["no"].ToString()),
                        1,
                        dataRow["dataTime"].ToString(),
                        dataRow["id"].ToString(),
                        dataRow["projectId"].ToString(),
                        dataRow["equipmentId"].ToString(),
                        dataRow["channelNo"].ToString(),
                        dataRow["calType"].ToString(),
                        dataRow["count"].ToString(),
                        dataRow["data1"].ToString(),
                        dataRow["data2"].ToString(),
                        dataRow["data3"].ToString(),
                        dataRow["data4"].ToString(),
                        dataRow["data5"].ToString(),
                        dataRow["data6"].ToString(),
                        dataRow["data7"].ToString(),
                        dataRow["data8"].ToString(),
                        dataRow["data9"].ToString(),
                        dataRow["data10"].ToString(),
                        dataRow["data11"].ToString(),
                        dataRow["data12"].ToString(),
                        dataRow["data13"].ToString(),
                        dataRow["data14"].ToString(),
                        dataRow["data15"].ToString(),
                        dataRow["data16"].ToString(),
                        dataRow["data17"].ToString(),
                        dataRow["data18"].ToString(),
                        dataRow["data19"].ToString(),
                        dataRow["data20"].ToString(),
                        dataRow["data21"].ToString(),
                        dataRow["data22"].ToString(),
                        dataRow["data23"].ToString(),
                        dataRow["data24"].ToString(),
                        dataRow["data25"].ToString(),
                        dataRow["data26"].ToString(),
                        dataRow["data27"].ToString(),
                        dataRow["data28"].ToString()
                        );
                        if (class_MySqlDataBase.ExecuteSql(Sql) > 0)
                        {

                        }
                        form_NewMain.SetProgressPosition(index++);
                        #endregion
                    }
                    #endregion

                    #region 导入
                    index = 0;
                    int ChannelNo = int.Parse(dataTable.Rows[0]["ChannelNo"].ToString());
                    string mainId = dataTable.Rows[0]["EquipmentId"].ToString();
                    int sensorNo = ChannelNo;
                    string equipmentId = dataTable.Rows[0]["equipmentId"].ToString();
                    string projectId = dataTable.Rows[0]["projectId"].ToString();
                    string calType = dataTable.Rows[0]["calType"].ToString();
                    double 温度补偿系数 = GetDefaultValue("temperatureCompensateParamer", sensorNo, mainId);
                    double 初始温度 = GetDefaultValue("iniTemperatureWave", sensorNo, mainId);
                    double[] 传感器初始波长 = GetOneArrayDefaultValue("iniNormalStressWave", sensorNo, mainId);
                    double[] 传感器标定系数 = GetOneArrayDefaultValue("normalStressCalibrationParamer", sensorNo, mainId);
                    double[] 传感器输出波长 = new double[7];
                    double 方位角α = GetDefaultValue("angleα", sensorNo, mainId);
                    double 倾角β = GetDefaultValue("angleβ", sensorNo, mainId);
                    double 旋转角γ = GetDefaultValue("angleγ", sensorNo, mainId);
                    double[,] 应力变换矩阵 = GetTwoArrayDefaultValue("powerChangeMatrix", sensorNo, mainId);
                    string batchOldId = null;
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string batchId = dataRow["id"].ToString();
                        if (batchOldId != batchId)
                        {
                            string[] vs = this.GetDelete(batchId, string.Format("_{0}", TimeSign));
                            foreach (string item in vs)
                                class_MySqlDataBase.ExecuteSql(item);
                            batchOldId = batchId;
                        }
                        int sortNo = 1;
                        Class_Cal cal = new Class_Cal();
                        double totalValue = 0;
                        if (calType == "正应力")
                        {
                            #region 第一道
                            for (int i = 0; i < 7; i++)
                            {
                                string ColName = string.Format("data{0}", i * 2 + 1);
                                传感器输出波长[i] = double.Parse(dataRow[ColName].ToString());
                                totalValue += 传感器输出波长[i];
                            }
                            if (Math.Abs(totalValue) > 0)
                            {
                                cal.设置初始值(
                                    温度补偿系数
                                    , 初始温度
                                    , 传感器初始波长
                                    , 传感器标定系数
                                    , 方位角α
                                    , 倾角β
                                    , 旋转角γ
                                    , 传感器输出波长
                                    , 应力变换矩阵);
                                Cal应力_Result cal应力 = cal.Get应力(null, batchId, equipmentId, projectId);
                                if (cal应力 != null)
                                {
                                    int length = cal应力.正应力.Length;
                                    for (int i = 0; i < length; i++)
                                    {
                                        if (i == 0)
                                        {
                                            string InsertTemp = cal应力.Get温度Insert(batchId, sensorNo.ToString()
                                                , 1
                                                , cal应力.温度, string.Format("_{0}", TimeSign));
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                        }
                                        string InsertSql = cal应力.Get应力Insert(batchId
                                            , sortNo++
                                            , cal应力.正应力[i]
                                            , cal应力.局部坐标系应力[i]
                                            , cal应力.ONE次变换[i]
                                            , cal应力.TWO次变换[i]
                                            , cal应力.THREE次变换[i]
                                            , cal应力.大地坐标系应力[i]
                                            , cal应力.li1[i]
                                            , cal应力.pqθ[i]
                                            , cal应力.主应力123[i]
                                            , cal应力.li2[i]
                                            , cal应力.mi2[i]
                                            , cal应力.ni2[i]
                                            , cal应力.主应力倾角β[i]
                                            , cal应力.主应力方位角α[i]
                                            , string.Format("_{0}", TimeSign)
                                            , null);
                                        InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                    }
                                }
                            }
                            #endregion

                            #region 第二道
                            totalValue = 0;
                            for (int i = 0; i < 7; i++)
                            {
                                string ColName = string.Format("data{0}", i * 2 + 1 + 14);
                                传感器输出波长[i] = double.Parse(dataRow[ColName].ToString());
                                totalValue += 传感器输出波长[i];
                            }
                            if (Math.Abs(totalValue) > 0)
                            {
                                cal.设置初始值(
                                    温度补偿系数
                                    , 初始温度
                                    , 传感器初始波长
                                    , 传感器标定系数
                                    , 方位角α
                                    , 倾角β
                                    , 旋转角γ
                                    , 传感器输出波长
                                    , 应力变换矩阵);
                                Cal应力_Result cal应力 = cal.Get应力(null, batchId, equipmentId, projectId);
                                if (cal应力 != null)
                                {
                                    int length = cal应力.正应力.Length;
                                    for (int i = 0; i < length; i++)
                                    {
                                        if (i == 0)
                                        {
                                            string InsertTemp = cal应力.Get温度Insert(batchId, sensorNo.ToString()
                                                , 2
                                                , cal应力.温度, string.Format("_{0}", TimeSign));
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                        }
                                        string InsertSql = cal应力.Get应力Insert(batchId
                                            , sortNo++
                                            , cal应力.正应力[i]
                                            , cal应力.局部坐标系应力[i]
                                            , cal应力.ONE次变换[i]
                                            , cal应力.TWO次变换[i]
                                            , cal应力.THREE次变换[i]
                                            , cal应力.大地坐标系应力[i]
                                            , cal应力.li1[i]
                                            , cal应力.pqθ[i]
                                            , cal应力.主应力123[i]
                                            , cal应力.li2[i]
                                            , cal应力.mi2[i]
                                            , cal应力.ni2[i]
                                            , cal应力.主应力倾角β[i]
                                            , cal应力.主应力方位角α[i]
                                            , string.Format("_{0}", TimeSign)
                                            , null);
                                        InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region 第一道
                            for (int i = 0; i < 7; i++)
                            {
                                string ColName = string.Format("data{0}", i * 2 + 1);
                                传感器输出波长[i] = double.Parse(dataRow[ColName].ToString());
                                totalValue += 传感器输出波长[i];
                            }
                            if (Math.Abs(totalValue) > 0)
                            {
                                cal.设置初始值(
                                温度补偿系数
                                , 初始温度
                                , 传感器初始波长
                                , 传感器标定系数
                                , 传感器输出波长);
                                Cal应变_Result cal应变 = cal.Get应变(null, batchId, equipmentId, projectId);
                                if (cal应变 != null)
                                {
                                    int length = cal应变.正应力.Length;
                                    for (int i = 0; i < length; i++)
                                    {
                                        if (i == 0)
                                        {
                                            string InsertTemp = cal应变.Get温度Insert(batchId, sensorNo.ToString()
                                                , 1
                                                , cal应变.温度, string.Format("_{0}", TimeSign));
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                        }
                                        string InsertSql = cal应变.Get应变Insert(batchId, sensorNo.ToString()
                                            , sortNo++
                                            , cal应变.正应力[i], string.Format("_{0}", TimeSign));
                                        InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                    }
                                }
                            }
                            #endregion

                            #region 第二道
                            totalValue = 0;
                            for (int i = 0; i < 7; i++)
                            {
                                string ColName = string.Format("data{0}", i * 2 + 1 + 14);
                                传感器输出波长[i] = double.Parse(dataRow[ColName].ToString());
                                totalValue += 传感器输出波长[i];
                            }
                            if (Math.Abs(totalValue) > 0)
                            {
                                cal.设置初始值(
                                温度补偿系数
                                , 初始温度
                                , 传感器初始波长
                                , 传感器标定系数
                                , 传感器输出波长);
                                Cal应变_Result cal应变 = cal.Get应变(null, batchId, equipmentId, projectId);
                                if (cal应变 != null)
                                {
                                    int length = cal应变.正应力.Length;
                                    for (int i = 0; i < length; i++)
                                    {
                                        if (i == 0)
                                        {
                                            string InsertTemp = cal应变.Get温度Insert(batchId, sensorNo.ToString()
                                                , 2
                                                , cal应变.温度, string.Format("_{0}", TimeSign));
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                        }
                                        string InsertSql = cal应变.Get应变Insert(batchId, sensorNo.ToString()
                                            , sortNo++
                                            , cal应变.正应力[i], string.Format("_{0}", TimeSign));
                                        InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                    }
                                }
                            }
                            #endregion
                        }
                        form_NewMain.SetProgressPosition(index++);
                    }

                    #endregion
                    form_NewMain.SetProgressOK();
                    if (InsertCount > 0)
                    {
                        MessageBox.Show(string.Format(@"成功计算出{0}条结果数据！", InsertCount)
                            , "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    //写入TXT文件，并打开
                    if (MessageBox.Show("发现不合法的数据;\r\n---您要查看数据验证日志吗？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                    {
                        string fileName = string.Format("{0}\\{1}", Application.StartupPath, "checklog.txt");
                        if (File.Exists(fileName))
                            System.Diagnostics.Process.Start(@fileName);
                    }
                }
            }
        }
        /// <summary>
        /// 得到删除计算结果SQL
        /// </summary>
        /// <param name="batchId"></param>
        /// <param name="Separation"></param>
        /// <returns></returns>
        private string[] GetDelete(string batchId, string Separation)
        {
            int len = 2;
            string[] vs = new string[len];
            vs[0] = string.Format(@"DELETE
                FROM output_temperature{1}
                WHERE batchId = '{0}'", batchId, Separation);
            vs[1] = string.Format(@"DELETE
                FROM output_data{1}
                WHERE batchId = '{0}'", batchId, Separation);
            return vs;
        }

        public override int DeleteData(object id)
        {
            base.GetGridPage().gridView1.FocusedRowHandle = -1;
            MessageBox.Show("校验成功！");
            return -1;
        }

        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            return null;
        }

        public override void Finder(int PageNo)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");
                DataTable newDataTable = new DataTable();
                newDataTable = dataTable.Clone();
                int begin = (PageNo - 1) * this.GetGridPage().PageSize;
                int end = PageNo * this.GetGridPage().PageSize - 1;
                if (dataTable.Rows.Count - 1 < end)
                {
                    end = dataTable.Rows.Count - 1;
                }
                while (begin < end + 1)
                {
                    DataRow dataRow = newDataTable.NewRow();
                    for (int i = 0; i < newDataTable.Columns.Count; i++)
                    {
                        if (dataTable.Rows[begin][i] != null)
                        {
                            dataRow[i] = dataTable.Rows[begin][i];
                        }
                    }
                    newDataTable.Rows.Add(dataRow);
                    begin++;
                }
                base.SetGridDataSource(newDataTable);
                base.GetGridPage().SetRowCount(dataTable.Rows.Count);
                waitDialogForm.Close();
            }

        }

        private void Form_strongPara_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void gridPage1_RowCellClickEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            GridView gridView = sender as GridView;
            if (e.Column.FieldName == "id")
            {
                string batchId = e.CellValue.ToString();
                if (batchId != null)
                {

                    Form_ResultCal form_ResultCal = new Form_ResultCal();
                    form_ResultCal.BatchId = batchId;
                    if (form_ResultCal.ShowDialog() == DialogResult.OK)
                    {

                    }
                    form_ResultCal.Close();
                    form_ResultCal.Dispose();
                }
            }
        }

    }
}
